package com.kit.data.kitopeso.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewbinding.ViewBinding;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.kit.data.kitopeso.PathMaping;
import com.kit.data.kitopeso.R;
import com.kit.data.kitopeso.activity.KitoCustomerServiceActivity;
import com.kit.data.kitopeso.activity.KitoPrivacyActivity;
import com.kit.data.kitopeso.activity.KitoSettingActivity;
import com.kit.data.kitopeso.activity.KitoUserActivity;
import com.kit.data.kitopeso.base.BaseActivity;
import com.kit.data.kitopeso.base.BaseFragment;
import com.kit.data.kitopeso.base.BaseParams;
import com.kit.data.kitopeso.databinding.KitoCenterFragmentBinding;
import com.kit.data.kitopeso.net.PU;
import com.kit.data.kitopeso.net.RxHttp;
import com.kit.data.kitopeso.net.U;
import com.kit.data.kitopeso.tools.AESCryptUtilJar;
import com.kit.data.kitopeso.tools.JsonParse;
import com.kit.data.kitopeso.tools.LogUtil;
import com.kit.data.kitopeso.tools.PhoneUtils;
import com.kit.data.kitopeso.tools.ResUtils;
import com.kit.data.kitopeso.tools.SpUtils;
import com.kit.data.kitopeso.tools.ViewUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class CenterFragment extends BaseFragment<KitoCenterFragmentBinding> {


    String name = "";
    String url;

    BaseActivity mActivity;


    @Override
    protected KitoCenterFragmentBinding initViewBinding(LayoutInflater inflater, ViewGroup container) {
        return KitoCenterFragmentBinding.inflate(inflater);
    }

    @Override
    protected void initView() {

        mActivity = (BaseActivity<ViewBinding>) getActivity();

        binding.getRoot().setPadding(0, 0, 0, 0);

        mActivity.setStatusBarHeight(binding.tvRoot);

        name = ResUtils.s(R.string.app_name);

        binding.tvCs.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P23, "");
            startActivity(new Intent(getActivity(), KitoCustomerServiceActivity.class));
        });
        binding.tvIconLayout.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P21, "");
            startActivity(new Intent(getActivity(), KitoUserActivity.class));
        });
        binding.tvUser1.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P25, "");
            startActivity(new Intent(getActivity(), KitoPrivacyActivity.class));
        });
        binding.tvUser2.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            PU.userBuriedPoint(PU.P24, "");
            startActivity(new Intent(getActivity(), KitoSettingActivity.class));
        });
    }

    @Override
    protected void initData() {
        UP_TYPE1 = PU.P18;
        UP_TYPE2 = PU.P19;
        setUserData();
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
        if ((PhoneUtils.isUpdate(getActivity()) || PhoneUtils.isForceUpdate(getActivity()))) {
            binding.tvDot.setVisibility(View.VISIBLE);
        } else {
            binding.tvDot.setVisibility(View.GONE);
        }
    }

    void setUserData() {
        if (getActivity() != null) {
            binding.tvPhone.setText(SpUtils.get_string(getActivity(), SpUtils.KEY_PHONE, ""));
            if (!TextUtils.isEmpty(name)) {
                binding.tvName.setText(name);
            }
            if (!TextUtils.isEmpty(url)) {
                Glide.with(binding.tvIcon.getContext()).load(url).placeholder(R.color.placeholder_icon).into(binding.tvIcon);
            }
        }
    }

    void getUserInfo() {
        JsonObject json = BaseParams.getJsonObject(getActivity(), PathMaping.PathList[75]);
        HashMap<String, String> heads = new HashMap<>();
        heads.put("product", U.URL_MAIN_PRODUCT);
        if (!TextUtils.isEmpty(U.URL_COUNTRY)) {
            heads.put("country", U.URL_COUNTRY);
        }
        RxHttp.post(U.commonUrl, AESCryptUtilJar.encryptDefault(json.toString()), heads)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        if (!TextUtils.isEmpty(s)) {
                            String json1 = AESCryptUtilJar.decryptDefault(s);
                            LogUtil.e("me", "getUserInfo json->" + json1);
                            if (JsonParse.getCode(json1)) {
                                JSONObject object = JsonParse.getAbUserInfoJson2Obj(json1);
                                if (object != null) {
                                    try {
                                        name = object.getString("name");
                                        url = object.getString("url");
                                    } catch (JSONException ignored) {
                                    }
                                }
                                setUserData();
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

}
